const router = require('koa-router')()
const jwt = require('../utils/jwt')

// token 唯一性怎么保证
// token 怎么加密
// 怎么设定过期时间
// 怎么校验前端传过来的token 的有效性

router.post('/login', (ctx) => {
     console.log(ctx.request.body);

    let user = ctx.request.body

    // 模拟一下数据库校验，假设账号密码没有问题
    if (ctx.request.body.username == 'luo' && ctx.request.body.password == 100 ) {
        let jwtToken = jwt.sign({ id: '1', username: user.username, admin: true })
        // console.log(jwtToken);
        ctx.body = {
            code: 0,
            data: user.username,
            token: jwtToken
        }
    } else if (ctx.request.body.username == 'guo' && ctx.request.body.password == 123) {
        let jwtToken = jwt.sign({ id: '2', username: user.username, admin: false })
        // console.log(jwtToken);
        ctx.body = {
            code: 0,
            data: user.username,
            token: jwtToken
        }
    } 
    else {
        ctx.body = {
            code: 1,
            data: '账号或密码错误,登录失败'
        }
    }
})

router.post('/home', jwt.verify(true), (ctx) => {
    try {
        ctx.body = {
            code: 0,
            data: 'Good me，美好的一天从奶茶开始吧',
        }

    } catch (error) {
        ctx.body = {
            code: 1,
            data: error
        }
    }
})



module.exports = router